package cc.vv.btpartyjob.delegate

import android.content.Context
import android.support.v4.app.FragmentTabHost
import android.support.v7.app.AppCompatActivity
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.TabHost
import android.widget.TextView
import cc.vv.btpartyjob.R
import cc.vv.mvp.delegate.BaseAppDelegate

/**
 * 公共TabHost视图托管类
 * Created by sun.li on 2018/9/5.
 * @author sun.li
 */
class BaseTableHostDelegate : BaseAppDelegate() {

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_base_tabhost
    }

    /**
     * 关联tab和fragment
     */
    fun initTab(mIVSrcArray: IntArray, mFragmentArray: Array<Class<*>>, mTabNames: Array<String>) {
        if (mIVSrcArray.size < mFragmentArray.size || mTabNames.size < mFragmentArray.size) {
            return
        }
        val mTabHost = getView<FragmentTabHost>(R.id.ft_btha_tabhost)
        mTabHost!!.setup(getActivity(), getActivity<AppCompatActivity>().supportFragmentManager, R.id.fl_btha_relContent)
        // 得到fragment的个数
        val fragmentCount = mFragmentArray.size
        for (i in 0 until fragmentCount) {
            // 为每一个Tab按钮设置图标、文字和内容
            val tabSpec: TabHost.TabSpec? = mTabHost.newTabSpec(mTabNames[i])?.setIndicator(
                    getTabItemView(mIVSrcArray, mTabNames, i))
            // 去除分割线
            mTabHost.tabWidget!!.dividerDrawable = null
            // 将Tab按钮添加进Tab选项卡中
            if (tabSpec != null) {
                mTabHost.addTab(tabSpec, mFragmentArray[i], null)
            }
        }

//        setTabClick(2, null)

    }

    /** 设置tabHost某个tab的点击事件*/
    fun setTabClick(index: Int, onClick: View.OnClickListener?) {
        val mTabHost = getView<FragmentTabHost>(R.id.ft_btha_tabhost)
        var tabItem: View? = null
        try {
            tabItem = mTabHost.tabWidget.getChildTabViewAt(index)
        } catch (e: Exception) {
        }
        tabItem!!.setOnClickListener(onClick)
    }

    /**
     * 设置tabHost图标和文字
     */
    private fun getTabItemView(mIVSrcArray: IntArray, mTabNames: Array<String>, index: Int): View {
        val text = mTabNames[index]
        val tabView = View.inflate(getActivity<AppCompatActivity>(), R.layout.layout_tabhost_button_content, null)
        val imageView = tabView.findViewById<ImageView>(R.id.iv_thbcl_image)
        if (mIVSrcArray[index] != -1) {
            imageView.setImageResource(mIVSrcArray[index])
        }
        val textView = tabView.findViewById<TextView>(R.id.tv_thbcl_text)
        if ("" == text) {
            textView.visibility = View.GONE
            var lp = imageView.layoutParams
            lp.width = dp2px(getActivity(), 38)
            lp.height = dp2px(getActivity(), 38)
            imageView.layoutParams = lp
        } else {
            textView.visibility = View.VISIBLE
        }
        textView.text = text
        return tabView
    }

    /**
     * 设置底部菜单
     */
//    fun initSectorMenuButton(mainBottomSectorMenu: IntArray) {
//        val menuButton = getView<SectorMenuButton>(R.id.bottom_sector_menu)
//        var buttonDatas = ArrayList<ButtonData>()
//        for (index in mainBottomSectorMenu.indices) {
//            val buttonData = ButtonData.buildIconButton(context, mainBottomSectorMenu[index], 0f)
//            buttonData.setBackgroundColorId(context, R.color.transparent)
//            buttonDatas.add(buttonData)
//        }
//        menuButton.buttonDatas = buttonDatas
//    }

    /**
     * 菜单点击监听
     */
//    fun setMenuClickEvent(buttonEventListener: ButtonEventListener) {
//        getView<SectorMenuButton>(R.id.bottom_sector_menu).setButtonEventListener(buttonEventListener)
//    }

    /**
     * dp转换成px
     */
    private fun dp2px(context: Context, dpValue: Int): Int {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue.toFloat(), context.resources.displayMetrics).toInt()
    }
}